/* 
 * File:   InteractiveMode.cpp
 * Author: taylor
 * 
 * Created on November 7, 2012, 2:33 PM
 */

#include "InteractiveMode.h"

/**
 * Enable/initialize this mode
 */
void InteractiveMode::enableMode() {
    int opt;
    enabled = true;

    outputOptions();

    while (/*cin >> opt  &&*/ enabled) {
        cin >> opt;
        getChosenOption(opt);
    }
}

/**
 * Output the options for Interactive mode
 */
void InteractiveMode::outputOptions() {
    cout << "What would you like to do?" << endl;
    cout << "\t1. Perform a search." << endl << endl;
    cout << "\t2. Switch Modes." << endl;
    cout << "\t3. Quit program." << endl;
}

/**
 * Get and parse the chosen option
 * @param opt
 */
void InteractiveMode::getChosenOption(int opt) {
    string str;

    switch (opt) {
        case 1: //Search
        {
            cout << "Enter your search query:" << endl;
            cin >> str;
            addDocument(str);

            outputOptions();
            break;
        }
        case 2: //Switch modes
        {
            enabled = false;
            return;
            break;
        }
        case 3: //Quit
        {
            exit(EXIT_SUCCESS);
        } 

    }
}

/**
 * Add and parse documents in a file
 * @param file
 */
void InteractiveMode::addDocument(string file) {
    //DocumentManager dm;
    dm->addDocumentsFromFile(file);
}

/**
 * Constructor asks the user if they want to use an AVL tree or Hash Table
 * @param dm_i
 */
InteractiveMode::InteractiveMode(DocumentManager* dm_i) {
    char opt;
    cout << "Would you like to use an AVL Tree or Hash Table? Enter \"a\" for AVL Tree or \"b\" for hash table: ";
    cin >> opt;

    if (opt == 'b') {
        dm_i = new DocumentManager('h');
        cout << "...Using hash table" <<endl;
    } else {
        dm_i = new DocumentManager('a');
        cout << "...Using AVL Tree" <<endl;
    }


    dm = dm_i;
}